package com.example.demoamqp.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.example.demoamqp.entity.Order;
import com.example.demoamqp.send.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @version 1.0.0
 * @className: TestSendMqController
 * @description: 消息发送者
 * @author: zhangjunfa
 * @date: 2023/6/16 11:05
 */
@Slf4j
@RequestMapping
@RestController
public class TestSendMqController {

    private Sender sender;
    private FanoutSender fanoutSender;

    private TopicSender topicSender;
    private DeadSender deadSender;
    private DelayQueueSender delayQueueSender;
    private Delay2Sender delay2Sender;


    public TestSendMqController(Sender sender, FanoutSender fanoutSender, TopicSender topicSender, DeadSender deadSender, DelayQueueSender delayQueueSender, Delay2Sender delay2Sender) {
        this.sender = sender;
        this.fanoutSender = fanoutSender;
        this.topicSender = topicSender;
        this.deadSender = deadSender;
        this.delayQueueSender = delayQueueSender;
        this.delay2Sender = delay2Sender;
    }


    @PostMapping("/send")
    public Object send(@RequestParam(name = "param") String param) throws InterruptedException {
        Thread.sleep(3000);
        sender.send(param);
        return "success";
    }

    @PostMapping("/sendSimple")
    public Object sendSimple(@RequestParam(name = "orderName") String orderName) throws InterruptedException {
        Order order = new Order();
        order.setId(IdUtil.getSnowflakeNextId());
        order.setOrderName(orderName);
        order.setOrderNo(IdUtil.nanoId());
        order.setCreatedTime(DateUtil.date());
        sender.sendSimple(order);
        return "success";
    }

    @PostMapping("/sendCode")
    public Object sendCode() throws InterruptedException {
        int randomInt = RandomUtil.randomInt(100000, 999999);
        log.info("生产者生成了一个验证码：{}", randomInt);
        this.fanoutSender.sendCode(String.valueOf(randomInt));
        return "success";
    }

    @PostMapping("/sendTopic")
    public Object sendTopic(@RequestParam(name = "msg") String msg, @RequestParam(name = "routingKey") String routingKey) throws InterruptedException {
        this.topicSender.sendMsg(msg, routingKey);
        return "success";
    }

    /**
     * 延迟队列（死信）设计
     *
     * @param msg
     * @return
     * @throws InterruptedException
     */
    @PostMapping("/sendDead")
    public Object sendDead(@RequestParam(name = "msg") String msg) throws InterruptedException {
        this.deadSender.sendDelay(msg, 2000);
        return "success 我是死信队列";
    }

    /**
     * 延迟队列设计
     *
     * @param msg
     * @return
     * @throws InterruptedException
     */
    @PostMapping("/sendDelay")
    public Object sendDelay(@RequestParam(name = "msg") String msg) throws InterruptedException {
        this.delayQueueSender.sendMsg(msg);
        return "success 我是延迟队列";
    }

    /**
     * 延迟队列设计
     *
     * @param msg
     * @return
     * @throws InterruptedException
     */
    @PostMapping("/sendDelay2")
    public Object sendDelay2(@RequestParam(name = "msg") String msg,@RequestParam(name = "delayTime") Integer delayTime) throws InterruptedException {
        this.delay2Sender.sendDelay2(msg,delayTime);
        return "success 我是延迟队列";
    }
}
